<template>
  <el-container class="home-container">
    <!--头部区域 -->
    <el-header>
      <div>
        <img
          src="../assets/logo.png"
          alt="logo图片"
          width="30px"
          height="30px"
        />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>

    <!-- 页面主体 -->
    <el-container>
      <!-- 左侧列表-->
      <el-aside :width="isCollapse ? '64px' : '200px'">

          <!-- 侧边栏的展开和折叠-->

        <div class="toggle-button" @click="toogelCollapse">|||</div>
        <!-- 左侧菜单栏
        
        是否折叠和展开
        :collapse="isCollapse"

        是否开启动画折叠
        :collapse-transition = "false 为了保证boolean值，一定要动态绑定
       
       router :开启侧边栏 的路由模式 **是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转
       
        :index="'/'+subitem.path"  当做跳转路径，，手动添加斜线
       
        -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#037cfe"
          :collapse="isCollapse"
          :collapse-transition = "false"
          router
          :default-active= "activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标-->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
                <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subitem.path" v-for="subitem in item.children" 
            :key="subitem.id" @click="saveNavState('/'+subitem.path)">
                   <!-- 二级菜单的模板区域 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容区 -->
      <el-main>

        <!--路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {

      return {
          menulist:[],
          iconsObj:{
              '125':'el-icon-s-data',
              '103':'el-icon-s-order',
              '101':'el-icon-s-home',
              '102':'el-icon-s-release',
              '145':'el-icon-s-promotion',

          },
          // 是否折叠
          isCollapse:false,
          // 被激活的连接地址
          activePath:'',
      }
  },

  // 页面被创建时，会执行次函数
  created(){

      this.getMenuList(),
    this.activePath = window.sessionStorage.getItem("activePath")
    

  },
  methods: {
    logout() {
      //  退出功能代码
      window.sessionStorage.clear()
      this.$router.push('/login')
    },

    async getMenuList(){

        // 获取菜单数据
        // data:
                // id authName order path children （表示自己的二级菜单）
        const{ data:res } = await this.$http.get('menus')
        console.log(res)

        // 渲染页面数据 data
        if(res.meta.status != 200) return this.$message.error(res.metha.msg)
        this.menulist = res.data
        


    },
    // 点击按钮,切换菜单栏的折叠与展开  menu 菜单属性
    toogelCollapse(){
        this.isCollapse = !this.isCollapse

    },
    // 保存连接的激活状态
    saveNavState(activePath){
        window.sessionStorage.setItem('activePath',activePath)
       
       // 点击菜单时，将数据保存到this.activePath中
       this.activePath = activePath

    }

  },
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}

.toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    line-height:24px;
    color:#fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>